<%
   technical_debt = measure('sqale_index')

   root_characteristics = Internal.debt.characteristics().to_a

   should_display_diff_measures = dashboard_configuration.selected_period? && technical_debt.variation(dashboard_configuration.period_index)!=nil
   if technical_debt.nil? || root_characteristics.empty?
%>

  <h3><%= message('widget.technical_debt_pyramid.name') -%></h3>
  <span style="color: #777777; font-size: 93%; font-style:italic"><%= message('widget.technical_debt_pyramid.no_info_available') -%></span>

<%
   else
%>
  <style>
    table.technicalDebtPyramid td {
      padding: 4px 10px 4px 0;
    }

    table.technicalDebtPyramid td.label {
      width: 1%;
      white-space: nowrap;
      text-align: left;
      padding: 5px 10px 5px 0;
    }

    table.technicalDebtPyramid th.val {
      width: 1%;
      white-space: nowrap;
      text-align: right;
      padding: 0 0 0 10px;
    }

    table.technicalDebtPyramid td.val {
      width: 1%;
      white-space: nowrap;
      text-align: right;
      padding: 0 0 0 10px;
    }

    table.technicalDebtPyramid td.bar {
      width: 97%;
    }

    table.technicalDebtPyramid .legendValue {
      width: 10px;
      height: 10px;
      background-color: #4b9fd5;
      display: inline-block;
      zoom: 1; /* IE hack to trigger hasLayout */
      *display: inline; /* IE hack to achieve inline-block behavior */
    }

    table.technicalDebtPyramid .legendTotal {
      width: 10px;
      height: 10px;
      background-color: #cae3f2;
      display: inline-block;
      zoom: 1; /* IE hack to trigger hasLayout */
      *display: inline; /* IE hack to achieve inline-block behavior */
    }

    table.technicalDebtPyramid td.bar>a>div>div {
      float: right;
    }
  </style>

  <table class="technicalDebtPyramid">
    <thead>
    <tr>
      <th colspan="2"><div class="marginbottom10">
        <h3><%= message('widget.technical_debt_pyramid.name') -%></h3>
      </div></th>
      <th class="val"><div class="marginbottom10">
        <h3><div class="legendValue"></div>&nbsp;<%= message('widget.technical_debt_pyramid.technical_debt') -%></h3>
      </div></th>
      <th class="val"><div class="marginbottom10">
        <h3><%= message('widget.technical_debt_pyramid.total') -%></h3>
      </div></th>
    </tr>
    </thead>
    <tbody>
    <%
       measures_by_characteristic_id={}
       ProjectMeasure.find(:all,
                           :conditions => ['characteristic_id IN (?) AND snapshot_id=? AND metric_id=?',
                                           root_characteristics.map{|c| c.id}, @snapshot.id, technical_debt.metric().id]).each do |measure|
         measures_by_characteristic_id[measure.characteristic_id]=measure
       end

       total = technical_debt.value
       cumulated=total

       if should_display_diff_measures
         diff_by_characteristic_id={}
         total_diff = 0;
         root_characteristics.each do |c|
           measure = measures_by_characteristic_id[c.id]
           if measure && measure.variation(dashboard_configuration.period_index)
             diff_by_characteristic_id[c.id] = measure.variation(dashboard_configuration.period_index)
             total_diff += measure.variation(dashboard_configuration.period_index)
           end
         end
       end

       root_characteristics.sort_by {|characteristic| characteristic.order}.each do |characteristic|
         measure=measures_by_characteristic_id[characteristic.id]
         value=(measure && measure.value ? measure.value : 0)
         total_size=(total>0 ? (100*cumulated/total).round(3): 0)
         value_size=(cumulated>0 ? (100*value/cumulated).round(3): 100)
         drilldown_url = url_for_drilldown('sqale_index', :characteristic => characteristic.key)
    %>
      <tr>
        <td class="label"><%= h(characteristic.name) -%></td>
        <td class="bar">
          <a href="<%= drilldown_url -%>">
            <div style="width: <%=total_size-%>%; height: 17px;background-color: #cae3f2;">
              <div style="width: <%=value_size-%>%; height: 17px;background-color: #4b9fd5;"></div>
            </div>
          </a>
        </td>
        <td class="val value-debt-<%= h(characteristic.key) -%>">
          <a href="<%= drilldown_url -%>" class="underlined-link link-debt-<%= h(characteristic.key) -%>"><%= Internal.i18n.formatLongDuration(value.to_i, 'SHORT') -%></a>
          <% if should_display_diff_measures %>
            <% if diff_by_characteristic_id[characteristic.id] %>
              <%= format_variation(measure) -%>
            <% else %>
              <span class="var"><b>(+0)</b></span>
            <% end %>
          <% end %>
        </td>
        <td class="val value-total-<%= h(characteristic.key) -%>">
          <%= Internal.i18n.formatLongDuration(cumulated.to_i, 'SHORT') -%>
          <%
             if should_display_diff_measures
               css_style = 'var'
               css_style += 'b' if total_diff < 0
               css_style += 'w' if total_diff > 0
               diff_to_display = (total_diff < 0 ? '' : '+') + Internal.i18n.formatLongDuration(total_diff.to_i, 'SHORT')
          %>
            <span class="<%= css_style -%>"><b>(<%= diff_to_display -%>)</b></span>
          <%
             total_diff -= diff_by_characteristic_id[characteristic.id] if diff_by_characteristic_id[characteristic.id]
             end
          %>
        </td>
      </tr>
    <%
       cumulated-=value
       end
    %>
    </tbody>
  </table>

<% end %>
